﻿#include "iQTextEdit.h"

void iQTextEdit::setMarkdown(const QString& markdown)
{
	QTextEdit::setMarkdown(markdown);
}



iQTextCursor iQTextEdit::textCursor() const
{
	return QTextEdit::textCursor();
}

void iQTextEdit::keyPressEvent(QKeyEvent* e)
{
	//调用父类的 keyPressEvent 函数处理回车键
	QTextEdit::keyPressEvent(e);
	if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter)
	{
		QSignalBlocker blocker(this->document());
		setCurrentCharFormat(QTextCharFormat());
		
	}
}
void iQTextEdit::handleCursorPositionChanged()
{
	qDebug() << "@@@@";
	if (!view_mode.base)return;
	
	qDebug() << "old:" << oldLineNumber << "new:" << this->textCursor().blockNumber();
	if (oldLineNumber != this->textCursor().blockNumber()|| view_mode.newChange) {
		view_mode.newChange = false;
		QSignalBlocker blocker(this->document());
		QTextBlock newBlock;
		int newPositionOfBlock;
		QString newArray;
		
		{
			iQTextCursor textCursor1 = this->textCursor();
			QSignalBlocker blocker1(textCursor1.document());
			//保存现场
			newBlock = this->textCursor().block();
			newPositionOfBlock = textCursor1.positionInBlock();
			textCursor1.setPosition(newBlock.position(), QTextCursor::MoveAnchor);
			textCursor1.select(QTextCursor::BlockUnderCursor);
			newArray = textCursor1.selection().toMarkdown();
			qDebug() << "newArray:" << newArray;
			
			//行号改变，修改原行格式
			qDebug() << document()->findBlockByNumber(oldLineNumber).position();
			textCursor1.setPosition(document()->findBlockByNumber(oldLineNumber).position(), QTextCursor::MoveAnchor);
			textCursor1.select(QTextCursor::BlockUnderCursor);
			QString array;
			//if (textCursor1.blockNumber())
				array = textCursor1.selection().toPlainText();
			//else
			//	array = textCursor1.selection().toMarkdown();
			qDebug() << array << "*";
			// 移动光标选中文本,插入Markdown格式文本
			textCursor1.beginEditBlock();
			textCursor1.movePosition(QTextCursor::StartOfBlock);
			textCursor1.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
			textCursor1.QTextCursor::insertMarkdown(array);
			//textCursor1.deleteChar();
			
			//设置新行格式
			textCursor1.setPosition(newBlock.position() + newPositionOfBlock, QTextCursor::MoveAnchor);
			//setCurrentCharFormat(QTextCharFormat());
			
			textCursor1.movePosition(QTextCursor::StartOfBlock);
			textCursor1.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
			//textCursor1.deleteChar();
			textCursor1.QTextCursor::insertText(newArray, QTextCharFormat());
			//textCursor1.deleteChar();
			//textCursor1.mergeBlockCharFormat(QTextCharFormat());
			textCursor1.endEditBlock();
			this->setTextCursor(textCursor1);
		}

		oldLineNumber = this->textCursor().blockNumber();
	}
	
}
QTextCursor& iQTextEdit::myInsertText(QString& content, QTextCursor& textCursor, ViewMode& mode)
{
	
	QSignalBlocker blocker(textCursor.document());


	// 移动光标到文本块的起始位置
	textCursor.movePosition(QTextCursor::StartOfBlock);
	// 扩展光标选区，直到文本块的结束位置
	textCursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);

	//textCursor.setBlockCharFormat(QTextCharFormat());

	if (mode.base) {//若选择的渲染模式为markdown
		//textCursor.insertMarkdown(content);

		//当前行渲染方式函数
		textCursor.insertText(content);
	}
	else if (!mode.base) {
		textCursor.insertText(content);
	}
	else {}//若后期拓展其他格式再添加

	return textCursor;

}
//void iQTextEdit::keyPressEvent(QKeyEvent* e)
//{
//
//}
//
//}
